<?php
/*
 * @Author: KingMin 
 * @Date: 2021-07-13 22:35:39 
 * @Last Modified by:   KingMin 
 * @Last Modified time: 2021-07-13 22:35:39 
 * @The authors website: https://www.kingmin.cn  
 */
/**
 * 导航
 * @copyright (c) KINGMIN All Rights Reserved
 */

class Menu_Model {
    private $db;
     private static $btnKey =array("查看"=>"index","编辑"=>"editor","新增"=>"add","删除"=>"del");
    function __construct() {
        $this->db = Database::getInstance();
    }
    // 菜单格式处理
    function menuJson($row){
        if(!is_array($row)){
            return false;
        }
        $id=$row['menu_id'];
        $roles=[];
        $roleslist=self::getAllroles();
        foreach ($roleslist as $k => $v) {
            if(in_array($id,$v['list'])){
                $roles[]=$v['key'];
            }
        }
        return $naviData = array(
                    'id' => $id,
                    "path"=>$row["path"],
                    "name"=>$row["menu_name"],
                    "component"=>$row["component"],
                    "redirect"=>$row["redirect"],
                    "meta"=>[
                        "title"=>$row["menu_title"],
                        "isLink"=>$row["isLink"],
                        "isHide"=>$row["isHide"]=='1'?true:false,
                        "isKeepAlive"=>$row["isKeepAlive"]=='1'?true:false,
                        "isAffix"=>$row["isAffix"]=='1'?true:false,
                        "isIframe"=>$row["isIframe"]=='1'?true:false,
                        "roles"=>$roles,
                        "icon"=>$row["icon"],
                        ],
                    );
    }
    // 获取后台所有的菜单
    function getAllmenu($rowlist='not'){
       $menu=[];
       $query = $this->db->query("SELECT * FROM `".DB_PREFIX."sys_menu` where `parent_id`='0' AND `visible`='0' ORDER BY order_num ASC"); 
        while ($row = $this->db->fetch_array($query)) {
            $mid=$row['menu_id'];
            $list=self::menuJson($row);
            $children=self::getChildren($mid,$rowlist);
            if($children){
                if($children['type']=="F"){
                    $ky=self::$btnKey;
                    $nl=[];
                    foreach($children['list'] as $k => $v){
                        // @$list=$ky[$row['menu_title']];
                        // $nl[]=$v;
                        if($rowlist!='not'){
                            if(in_array($v['id'],$rowlist)){
                                // $menu[] = $list??[];
                                @$nl[]=$ky[$v['meta']['title']];
                            }
                        }else{
                            @$nl[]=$ky[$v['meta']['title']];
                        }
                    }
                    $list['meta']['roleBtn']=$nl;
                }else{
                    $list['children']=$children['list'];
                }
            }
            if($rowlist!='not'){
                if($row['menu_name']=='home'){
                    $menu[] = $list??[];
                }else{
                    if(in_array($mid,$rowlist )){
                        $menu[] = $list??[];
                    }
                }
            }else{
                $menu[] = $list??[];
            }
        }
       return $menu;
    }
    
    // 获取是否有子菜单
    function getChildren($id,$rowlist='not'){
        if(empty($id)){
            return false;
        }
        $query=$this->db->query("SELECT * FROM `".DB_PREFIX."sys_menu` where `parent_id`='$id'  AND `visible`='0' ORDER BY order_num ASC");
        $num=$this->db->num_rows($query);
        if($num){
            $menu=[];$type="C";
            while ($row = $this->db->fetch_array($query)) {
                $mid=$row['menu_id'];
                $type=$row['menu_type'];
                $list=self::menuJson($row);
                $children=self::getChildren($mid);
                if($children){
                    if($children['type']=="F"){
                        $ky=self::$btnKey;
                        $nl=[];
                        foreach($children['list'] as $k => $v){
                            // @$list=$ky[$row['menu_title']];
                            // $nl[]=$v;
                            if($rowlist!='not'){
                                if(in_array($v['id'],$rowlist)){
                                    // $menu[] = $list??[];
                                    @$nl[]=$ky[$v['meta']['title']];
                                }
                            }else{
                                @$nl[]=$ky[$v['meta']['title']];
                            }
                        }
                        $list['meta']['roleBtn']=$nl;
                    }else{
                        $list['children']=$children['list'];
                    }
                }
                if($rowlist!='not'){
                    if(in_array($mid,$rowlist)){
                        $menu[] = $list??[];
                    }
                }else{
                    $menu[] = $list??[];
                }
            }
            return array("type"=>$type,"list"=>$menu);
        }else{
            return false;
        }
    }
    // 缓存各个权限的菜单
    function getAllrolemenu(){
        $roles = array();
        $query = $this->db->query("SELECT * FROM `".DB_PREFIX."sys_role` ");
        while ($row = $this->db->fetch_array($query)) {
            $m=self::getrolemenu($row['role_id'])??[];
            $roles[]=[
                'role_id'=>$row['role_id'],
                'name'=>$row['role_name'],
                'key'=>$row['role_key'],
                'list'=>self::getAllmenu($m),
            ];
        }
        return $roles;
    }
    // 判断是否有权限
    function getAllroles(){
        $roles = array();
        $query = $this->db->query("SELECT * FROM `".DB_PREFIX."sys_role` ");
        while ($row = $this->db->fetch_array($query)) {
            $roles[]=[
                'role_id'=>$row['role_id'],
                'name'=>$row['role_name'],
                'key'=>$row['role_key'],
                'list'=>self::getrolemenu($row['role_id']),
            ];
        }
        return $roles;
    }
    function getrolemenu($id){
        $rolemenu = array();
        $query = $this->db->query("SELECT * FROM `".DB_PREFIX."sys_role_menu` where `role_id`='$id'");
        while ($row = $this->db->fetch_array($query)) {
            $rolemenu[]=$row['menu_id'];
        }
        return $rolemenu;
    }
}